package org.jeecg.modules.gather.seaweedfs;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@EnableConfigurationProperties(SeaweedFSProperties.class)
public class SeaweedFSUtil {
    @Resource
    private SeaweedFSProperties seaweedFSProperties;
    @Resource
    private RestTemplate restTemplate;

    /**
     * 上传接口
     * @param path path
     * @param fileName 文件名
     * @param fileBytes 文件内容
     * @return 访问路径
     */
    public Map<String, String> uploadFileContent(String path, String fileName, byte[] fileBytes) {
        String[] splitFileName = fileName.split("\\.");
        int length = splitFileName.length - 1;
        String fileType = splitFileName[length];
        path = path != null ? path : seaweedFSProperties.getTemporarilyPath();
        String filePath = path;
        if (fileType.equals("pdf"))
            filePath = seaweedFSProperties.getHtmlToPdfPath();
        else if (fileType.equals("html"))
            filePath = seaweedFSProperties.getWebPath();
        String uploadUrl = seaweedFSProperties.getUrl() + "/" + filePath + "/";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        ByteArrayResource fileResource = new ByteArrayResource(fileBytes) {
            @Override
            public String getFilename() {
                return fileName;
            }
        };

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", fileResource);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

        try {
            restTemplate.postForEntity(uploadUrl, requestEntity, String.class);
        } catch (RestClientException e) {
            log.error("请求SeaweedFS失败", e);
        }

        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("filePath", filePath);
        returnMap.put("fileName", fileName);
        return returnMap;
    }

    /**
     * 判断文件是否存在
     * @param url 文件路径
     * @return 是否存在
     */
    public boolean isFileExists(String url) {
        try {
            ResponseEntity<Void> response = restTemplate.exchange(
                    url,
                    HttpMethod.HEAD,
                    null,
                    Void.class
            );
            return response.getStatusCode() == HttpStatus.OK;
        } catch (Exception e) {
            return false;
        }
    }
}
